﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Models;
using DVSProject.Lib;
namespace DVSProject.Controllers
{
    public class ManageResultRemarkController : Controller
    {
        //
        // GET: /ManageResultRemark/

        public ActionResult Index()

        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }   
            ResultRemarkRepo resultRemarkRepo =new ResultRemarkRepo();
            IEnumerable<ResultRemarkModels> resultRemarks =resultRemarkRepo.GetResultRemark();
           
            return View(resultRemarks);
            
        }

        //
        // GET: /ManageResultRemark/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ResultRemarkRepo _ResultRemarkRepo = new ResultRemarkRepo();
            ResultRemarkModels _ResultRemark = new ResultRemarkModels();
            _ResultRemark = _ResultRemarkRepo.GetResultRemarkById(id);
            return View(_ResultRemark);
        }

        //
        // GET: /ManageResultRemark/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ResultRemarkModels _ResultRemark = new ResultRemarkModels();

            TitleResultRepo _TitleResultRepo = new TitleResultRepo();
            IEnumerable<TitleResultModels> _TitleResults = _TitleResultRepo.GetTitleResult();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _TitleResults
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleResultId == selectedId),
                                                         Text = s.TitleResultName,
                                                         Value = s.TitleResultId.ToString()
                                                     };

            //_Machine.ModelMacs = _Models;
            _ResultRemark.TitleResultSelect = selectlist;
            return View(_ResultRemark);
        } 

        //
        // POST: /ManageResultRemark/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                ResultRemarkRepo resultRemarkRepo = new ResultRemarkRepo();
                ResultRemarkModels resultRemark =new ResultRemarkModels();
                resultRemark.TitleResultId = (!String.IsNullOrEmpty(collection["TitleResultId"])) ? Convert.ToInt32(collection["TitleResultId"]) : 0;
                resultRemark.ResultRemarkTh =collection["ResultRemarkTh"];
                resultRemark.ResultRemarkEng =collection["ResultRemarkEng"];
                resultRemark.CreateBy = (string)Session["User"];
               
                bool result = resultRemarkRepo.Create(resultRemark);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ManageResultRemark/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ResultRemarkRepo resultRemarkRepo = new ResultRemarkRepo();
            ResultRemarkModels resultRemark = resultRemarkRepo.GetResultRemarkById(id);

            TitleResultRepo _TitleResultRepo = new TitleResultRepo();
            IEnumerable<TitleResultModels> _TitleResults = _TitleResultRepo.GetTitleResult();
            int selectedId = 1;
            IEnumerable<SelectListItem> selectlist = from s in _TitleResults
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.TitleResultId == selectedId),
                                                         Text = s.TitleResultName,
                                                         Value = s.TitleResultId.ToString()
                                                     };

            //_Machine.ModelMacs = _Models;
            resultRemark.TitleResultSelect = selectlist;
            return View(resultRemark);
        }

        //
        // POST: /ManageResultRemark/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                ResultRemarkRepo resultRemarkRepo = new ResultRemarkRepo();
                ResultRemarkModels resultRemark = new ResultRemarkModels();
                resultRemark.Id = id;
                resultRemark.TitleResultId = (!String.IsNullOrEmpty(collection["TitleResultId"])) ? Convert.ToInt32(collection["TitleResultId"]) : 0;
                resultRemark.ResultRemarkTh = collection["ResultRemarkTh"];
                resultRemark.ResultRemarkEng = collection["ResultRemarkEng"];
                resultRemark.UpdateBy = (string)Session["User"];
                bool result = resultRemarkRepo.Edit(resultRemark);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ManageResultRemark/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ResultRemarkRepo _ResultRemarkRepo = new ResultRemarkRepo();
            ResultRemarkModels _ResultRemark = new ResultRemarkModels();
            _ResultRemark = _ResultRemarkRepo.GetResultRemarkById(id);
            return View(_ResultRemark);
        }

        //
        // POST: /ManageResultRemark/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                ResultRemarkRepo _ResultRemarkRepo = new ResultRemarkRepo();
                ResultRemarkModels _ResultRemark = new ResultRemarkModels();
                _ResultRemark.Id = id;
                _ResultRemark.UpdateBy = (string)Session["User"];
                bool result = _ResultRemarkRepo.Delete(_ResultRemark);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
